<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model.trim="listQuery.staffName" placeholder="输入用户名" clearable style="width: 350px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-button class="filter-item" type="primary" style="margin-left: 10px;" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="danger" icon="el-icon-edit" @click="handleCreate">
        新增
      </el-button>
    </div>
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="tableData"
      border
      fit
      :default-sort="{prop:'createTime',order:'descending'}"
      highlight-current-row
      style="width: 100%"
    >
      <el-table-column label="序号" type="index" align="center" width="120">
        <template slot-scope="scope">
          <span>{{ (listQuery.pageNo-1)*listQuery.pageSize+scope.$index+1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="staffId" align="center">
        <template slot-scope="{row}">
          <span>{{ row.staffId }}</span>
        </template>
      </el-table-column>
      <el-table-column label="客户名称" prop="staffName" width="300" align="center">
        <template slot-scope="{row}">
          <span>{{ row.staffName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="时间" prop="createTime" sortable align="center">
        <template slot-scope="{row}">
          <span>{{ row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作人" prop="createUserName" align="center" width="120">
        <template slot-scope="{row}">
          <span>{{ row.createUserName }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="{row,$index}">
          <el-button type="primary" size="mini" @click="handleUpdate(row)">
            修改
          </el-button>
          <el-button v-if="row.status!='deleted'" size="mini" type="danger" @click="handleDelete(row,$index)">
            删除
          </el-button>
        </template>
      </el-table-column> -->
    </el-table>
    <!-- 分页 -->
    <div class="pageGation">
      <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNo" :limit.sync="listQuery.pageSize" @pagination="getCustomerList" />
    </div>
    <!-- 弹窗 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="100px" style="width: 400px; margin-left:50px;">
        <el-form-item label="客户名称" prop="staffName">
          <el-input v-model="temp.staffName" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          确定
        </el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>

import Pagination from '@/components/Pagination' // secondary package based on el-pagination
// import parseTime from '@/utils/index'
import { CustomerList, addCustomerStaff } from '@/api/customer'
export default {
  components: {
    Pagination
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        use: 'success',
        disabled: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      tableData: [],
      total: 0,
      currentRow: null,
      listQuery: {
        pageNo: 1,
        pageSize: 20,
        staffName: ''
      },
      listLoading: false,
      tableKey: 0,
      dialogStatus: 'create',
      dialogFormVisible: false,
      textMap: {
        edit: '修改',
        create: '新增'
      },
      temp: {
        staffName: ''
      },
      statusOptions: ['use', 'disabled', 'deleted'],
      rules: {
        staffName: [{ required: true, message: '客户名称不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    //
    // this.tableData.forEach(item=>{
    //   if(item.timestamp){
    //     this.temp.timestamp=parseTime(item.timestamp)
    //   }
    // })
    this.getCustomerList()
  },
  methods: {
    // 获取代理商列表
    async getCustomerList() {
      this.listLoading = true
      await CustomerList(this.listQuery).then(res => {
        if (res.code === '00000') {
          this.listLoading = false
          this.tableData = res.data.rows
          this.total = res.data.totalRows
        } else {
          this.$message('请求失败')
          this.listLoading = false
        }
      })
    },
    handleCreate() {
      this.dialogFormVisible = true
    },
    // 新增数据
    handleaddCustomer() {
      this.dialogFormVisible = true
      const { staffName } = this.temp
      if (!staffName) {
        this.$message('账户不能为空')
        return
      }
      this.listLoading = true
      addCustomerStaff(this.temp).then(res => {
        if (res.code === '00000') {
          this.$message.success('新增成功')
          setTimeout(() => {
            this.getCustomerList()
            this.listLoading = false
            this.dialogFormVisible = false
            this.temp = {}
          }, 500)
        } else {
          this.listLoading = false
          this.$message('请求失败')
        }
      })
    },

    // 搜索
    handleFilter() {
      this.listLoading = true
      setTimeout(() => {
        this.getCustomerList()
        this.listLoading = false
      }, 200)
    },
    // 修改
    handleUpdate(row) {
      this.temp = Object.assign({}, row)
      this.temp.timestamp = new Date(this.temp.timestamp)
      this.dialogStatus = 'edit'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleDelete() {
    },
    createData() {
      this.handleaddCustomer()
    }

  }
}
</script>
<style lang="scss" scoped>
.el-table{
  position: relative;
    overflow: hidden;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    -webkit-box-flex: 1;
    -ms-flex: 1;
    flex: 1;
    width: 100%;
    max-width: 100%;
    background-color: #fff;
    font-size: 14px;
    color: #606266;
}
.el-button--mini {
    padding: 7px 10px;
    min-width: 60px;
  }
  .el-button {
    font-size: 14px;
    border-radius: 4px;
}
.pageGation{
  display: flex;
  justify-content: flex-end;
  margin: 15px 40px 0;
  font-size: 23px;
}
</style>
